///////////////////////////////////////////////////////////////////////////// GOAL: urvival analysis 
// datasets for future use, prepare data for STSET
//		- ""ICB_MIDIP_joinby_forSTSET.dta - file ready for STSET 
//////////////////////////////////////////////////////////////////////////

// ready for stset
///use "ICB_MIDIP_joinby_forSTSET.dta", replace 
cd "C:\Users\006501608\Dropbox\Analysis\LB21" 
use "ICB_MIDIP_joinby_forSTSET_temp25dem.dta", clear  
// gen id variable 
gen id = prevcrisno+dyad
order id
// from previous event
drop eventime_y
gen eventime_y = eventime/365.25
label variable eventime_y "Time to crisis recurrence, in years"
stset eventime_y, fail(recurred) exit(time .) enter(time0) id(id)
// stset eventime, fail(recurred) exit(time .) enter(time0)
order _st _d recurred _t eventime_y eventime _t0 time0

// Fig. 1
by dyad: gen count = _n
by dyad: gen total = _N
tab total if count ==1 // 51 dyads with one crisis only 
tab total if count ==1 & total>1 
list dyad dyadname c4 recurred total count if total==6, sepby(dyad) // South and North Koreas take the lead with six crises
list dyad dyadname c4 recurred total count if total>4, sepby(dyad) // dyads with five repeared crises 


// Fig. 2
sts graph, ci 
sts list, at(20)

// H1 - presence of military hostile acts anytime during postctisis period
sts test x1, logrank // not significant 
sts test x1, logrank strata(dyad)

// H2 - timining of military hostile acts ("gap" - days from start of postcrisis phase to the start of the first incident)
gen gapall = .
replace gapall=gap if x1==1
replace gapall=eventime if x1==0 & gapall==.
stcox gapall // significant 

// H3 - number of military incidents  
tab x2postHnum, mis 
stcox x2postHnum // not significant 

// H4  - duration of military incidents 
tab totaldur, mis
replace totaldur=0 if totaldur==. & x1postH==0 // 0 duration if no postcrisis hostility happened 
stcox totaldur // not significant 

// H5 - severity of military incidents
replace x6postHhostlev=0 if x6postHhostlev==. & x1postH==0 // 0 severity if no postcrisis hostility happened 
recode x6postHhostlev (0=0) (2/3=1) (4=2), gen(x6group)
tab x6group, mis // 0 - no incidents, 1 - no use of force incidents, 2 - use of force incidents 
sts test x6group, logrank // not significant 

// Postcrisis hostility within one, two, or three years, or after three years 
tab gap1y, mis
recode gap1y 1=1 2=0 .=0, gen(gap1ybi)
tab gap1ybi, mis // in 56 cases, postcrisis hostility started within one year after the end of a crisis 
tab gap2y, mis
recode gap2y 1=1 2=0 .=0, gen(gap2ybi) 
tab gap2ybi, mis //in 69 cases, postcrisis hostility started within two years after the end of a crisis
recode gap3y 1=1 2=0 .=0, gen(gap3ybi) 
tab gap3ybi, mis //in 73 cases, postcrisis hostility started within three years after the end of a crisis

gen after3 = . 
	replace after3=0 if x1==0 & after3==.
	replace after3=1 if x1==1 & gap3ybi!=1 & after3==.
tab after3, mis
recode after3 0=0 .=1 1=2, gen (after3B) // 38 cases never involved hostility, 6 cases had hostility after three years, 73 cases - started within the first three years 


// H1 - presence of military hostile acts at different stages of postctisis period
	** within first year
sts test gap1ybi, logrank // significant 
sts list, by(gap1ybi) compare
stsum, by(gap1ybi)
	** within two years
stsum, by(gap2ybi)
sts list, by(gap2ybi) 
sts list, by(gap2ybi) compare
sts test gap2ybi, logrank // significant 
	** within three years
stsum, by(gap3ybi)
sts list, by(gap3ybi) compare
sts list, by(gap3ybi) 
sts test gap3ybi, logrank // significant 
		** graph to compare all above
sts graph, by (gap1ybi) name (graph1, replace) // hostility started within one year 
sts graph, by (gap2ybi) name (graph2, replace) // hostility started within two years 
sts graph, by (gap3ybi) name (graph3, replace) // hostility started within three years 
sts graph, by (x1) name (graph4, replace)
graph combine graph1 graph2 graph3 graph4


*****************************************************
// key independent variables: 
	// gap1ybi - Postcrisis military hostilities (within 1 year after the end of crisis) 
	// totalnum1y - Number of postcrisis military incidents (within the first year)
	replace totalnum1y = 0 if totalnum1y==.
	tab totalnum1y, mis
	 // totaldur1y - Duration of postcrisis military incidents (within 1 year after the end of crisis)
	replace totaldur1y = 0 if totaldur1y==.
	tab totaldur1y, mis
	// hostlev1y - Level of hostility of the incident (within 1 year after the end of crisis)
	recode hostlev1y .=0 2/3=1 4=2 // 0 - none, 1 - display or use of force, 2 - use of force 
	tab hostlev1y, mis

// control variables 	
// dem2 - Democratic dyad anytime during crisis 
recode dem2 0=1 1=0 // 0 - nondemocratic, 1 - democratic 
// riv1  - Rivalry all the time during postctisis period
// c1pastout - Settlement outcome in the past crisis 
// c4pastnumcr "Previous crises" number of past crises 
// icbviol - "Violence level" in past crisis 
// icbbrexit - "Crisis duration"
// icbcract - "Number of crisis actors"
// manipmed - "Maniplative mediation"
// med 	 - mediation 
recode icbmedstyle 4=1 1/3=0 .=0, gen(manipmed) // 1 - manipulative mediation, 0 - no manip med 
recode icbmedstyle 2/4=1 1=0 .=0, gen(med) // 1 - mediation, 0 - no med 


* estimate model
// Table 1
// Model 1 - GAP1BI - Presence as KEY IV 
*Original model specification
eststo clear
stcox gap1ybi c1pastout c4pastnumcr icbviol icbbrexit icbcract med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED
estat phtest, detail
*is it a problem with stsplit (see Jin/Boehmke 2017)? -->looks like it, coefficients are different with the adequate procedure in Stata
stcox gap1ybi c1pastout c4pastnumcr icbviol icbbrexit icbcract med dem2 riv1, strata(str) cluster(dyad) efron nohr texp(_t) tvc(gap1ybi c1pastout c4pastnumcr icbviol icbbrexit icbcract med dem2 riv1)
*In order to be sure, I use the stsplit command
stsplit, at(failures)
foreach var in gap1ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1 {
gen `var'_t= `var'*_t
}
*rerun with correctly split variables 
stcox gap1ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1 gap1ybi_t c1pastout_t c4pastnumcr_t icbviol_t icbbrexit_t icbcractr_t med_t dem2_t riv1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED for manip2
estat phtest, detail
*rerun without interactions
stcox gap1ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED for riv1
estat phtest, detail
*rerun -- add interaction with riv1  ->save results for table
eststo, title(Model 1): stcox gap1ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1 riv1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> PERFECT
estat phtest, detail

// Model 2 /

// gap2yb - Presence within two years  
*Original model specification
stcox gap2ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED
estat phtest, detail
*is it a problem with stsplit (see Jin/Boehmke 2017)? -->looks like it, coefficients are different with the adequate procedure in Stata
stcox gap2ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr texp(_t) tvc(gap2ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1)
*In order to be sure, I use the manual work around with the stsplit command
stsplit, at(failures)
foreach var in gap2ybi {
gen `var'_t= `var'*_t
}
*rerun with correctly split variables ->save results for table
stcox gap2ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1 gap2ybi_t c1pastout_t c4pastnumcr_t icbviol_t icbbrexit_t icbcractr_t med_t dem2_t riv1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED 
estat phtest, detail
*rerun without interactions
stcox gap2ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED for riv1
estat phtest, detail
*rerun -- add interaction with riv1  ->save results for table
eststo, title(Model 2): stcox gap2ybi c1pastout c4pastnumcr icbviol  icbbrexit icbcractr med dem2 riv1 riv1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> PERFECT
estat phtest, detail

// Model 3

// gap3ybi - Presence within three years  
*Original model specification
stcox gap3ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED
estat phtest, detail
*is it a problem with stsplit (see Jin/Boehmke 2017)? -->looks like it, coefficients are different with the adequate procedure in Stata
stcox gap3ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr texp(_t) tvc(gap3ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1)
*In order to be sure, I use the manual work around with the stsplit command
stsplit, at(failures)
foreach var in gap3ybi {
gen `var'_t= `var'*_t
}
*rerun with correctly split variables ->save results for table
stcox gap3ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1 gap3ybi_t c1pastout_t c4pastnumcr_t icbviol_t icbbrexit_t icbcractr_t med_t dem2_t riv1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED
estat phtest, detail
*rerun without interactions
stcox gap3ybi c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED for riv1
estat phtest, detail
*rerun -- add interaction with riv1  ->save results for table
eststo, title(Model 3): stcox gap3ybi c1pastout c4pastnumcr icbviol  icbbrexit icbcractr med dem2 riv1 riv1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> PERFECT
estat phtest, detail

// Model 4

// x1 - after3
*Original model specification
stcox after3 c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED
estat phtest, detail
*is it a problem with stsplit (see Jin/Boehmke 2017)? -->looks like it, coefficients are different with the adequate procedure in Stata
//stcox after3 c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr texp(_t) tvc(after3 c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1)
*In order to be sure, I use the manual work around with the stsplit command
stsplit, at(failures)
foreach var in after3 {
gen `var'_t= `var'*_t
}
*rerun with correctly split variables ->save results for table
//stcox after3  c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1 after3_t c1pastout_t c4pastnumcr_t icbviol_t icbbrexit_t icbcractr_t med_t dem2_t riv1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED for
estat phtest, detail
*rerun without interactions
stcox after3 c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED for riv1
estat phtest, detail
*rerun -- add interaction with riv1  ->save results for table
eststo, title(Model 4): stcox after3 c1pastout c4pastnumcr icbviol  icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> PERFECT
estat phtest, detail

// Model 5
 
// x1 - Anytime
*Original model specification
stcox x1 c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED
estat phtest, detail
*is it a problem with stsplit (see Jin/Boehmke 2017)? -->looks like it, coefficients are different with the adequate procedure in Stata
stcox x1 c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr texp(_t) tvc(x1 c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1)
*In order to be sure, I use the manual work around with the stsplit command
stsplit, at(failures)
foreach var in x1 {
gen `var'_t= `var'*_t
}
*rerun with correctly split variables ->save results for table
stcox x1postH  c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1 x1_t c1pastout_t c4pastnumcr_t icbviol_t icbbrexit_t icbcractr_t med_t dem2_t riv1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED for manip2
estat phtest, detail
*rerun without interactions
stcox x1postH c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> VIOLATED for riv1
estat phtest, detail
*rerun -- add interaction with riv1  ->save results for table
eststo, title(Model 5): stcox x1postH c1pastout c4pastnumcr icbviol  icbbrexit icbcractr med dem2 riv1 riv1_t x1_t, strata(str) cluster(dyad) efron nohr
*evaluate proportional hazards assumption --> PERFECT
estat phtest, detail

// Create a table with word doc for all the five models 

esttab using _table6.rtf, star(* 0.1 ** 0.05 *** 0.01)  b(3) se(3)  mtitle nodepvar  /*
*/ 		order(gap1ybi gap2ybi gap3ybi after3 x1postH  c1pastout c4pastnumcr icbviol icbbrexit icbcractr med dem2 riv1 riv1_t x1_t) noobs replace /*
*/		scalar("N_sub Cases" "N_fail Failures") /*
*/ 		varlabels(gap1ybi "Within 1 year"/*
*/ 		gap2ybi "Within 2 years"/*
*/ 		gap3ybi "Within 3 years"/*
*/ 		x1postH "Anytime"/*
*/ 		c1pastout "Settlement outcome"/*
*/		c4pastnumcr "Previous crises"/*
*/ 		icbviol "Violence level"/*
*/ 		icbbrexit "Crisis duration"/*
*/ 		icbcractr "Number of actors"/* 
*/ 		med "Mediation"/* 
*/ 		dem2 "Democratic dyad"/*
*/ 		riv1 "Strategic rivals"/*
*/ 		riv1_t "Rivals * time"/* 
*/ 		x1_t "Anytime * time"/* 
*/		_cons Constant) nonotes  /*
*/ 		addnotes("cluster robust s.e., * p < 0.1, ** p < 0.05, *** p < 0.01")



save "ICB_MIDIP_STSET_temp.dta", replace  

exit 


